package com.renren.webmaven2.controllers;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.view.RedirectView;

import com.renren.component.utils.HttpUtils;
import com.renren.eliya.domain.BigClass;
import com.renren.eliya.domain.BreadPic;
import com.renren.eliya.domain.BreadPrice;
import com.renren.eliya.domain.JoinUs;
import com.renren.eliya.domain.MyBread;
import com.renren.eliya.domain.MyComment;
import com.renren.eliya.domain.SmallClass;
import com.renren.webmaven2.domain.User;
import com.renren.webmaven2.service.BigClassService;
import com.renren.webmaven2.service.BreadService;
import com.renren.webmaven2.service.SmallClassService;
import com.renren.webmaven2.util.CacheUtils;
import com.renren.webmaven2.util.Constants;
import com.renren.yx.base.dao.daoutil.InitDao;

/**
 */
@Controller
@SuppressWarnings("unchecked")
public class AdminBrandController extends LocalBaseController {

	@Autowired
	private BigClassService bigClassService;
	@Autowired
	private SmallClassService smallClassService;
	@Autowired
	private BreadService breadService;

	@RequestMapping(value = "admin_bread")
	public String adminBread(HttpServletRequest request, Model model) {

		User user = (User) request.getSession().getAttribute(Constants.USER_COOKIE);
		if (user == null) {
			model.addAttribute("gourl", "admin_bread");
			return "login";
		}

		getClasses(model);
		String name = getParameter(request, "name");
		String code = getParameter(request, "code");
		int pageNo = getParameterInt(request, "pageNo");
		List<MyBread> myBreads = breadService.findBreads(name, code, pageNo);
		model.addAttribute("breads", myBreads);
		String id = getParameter(request, "id");
		if (StringUtils.isNotBlank(id)) {
			MyBread bread = (MyBread) InitDao.getDao().getEntity(MyBread.class, Integer.valueOf(id));
			model.addAttribute("bread", bread);
		}
		model.addAttribute("name", name);
		model.addAttribute("code", code);
		model.addAttribute("pageNo", (pageNo <= 0 ? 1 : pageNo));
		return "admin_bread";
	}

	@RequestMapping(value = "viewComments")
	public String viewComments(HttpServletRequest request, Model model) {

		User user = (User) request.getSession().getAttribute(Constants.USER_COOKIE);
		if (user == null) {
			model.addAttribute("gourl", "viewComments");
			return "login";
		}

		List<MyComment> comments = InitDao.getDao().getEntities(MyComment.class);
		model.addAttribute("comments", comments);
		return "viewComments";
	}

	@RequestMapping(value = "viewJoinUs")
	public String viewJoinUs(HttpServletRequest request, Model model) {
		//asdfasdfas
		User user = (User) request.getSession().getAttribute(Constants.USER_COOKIE);
		if (user == null) {
			model.addAttribute("gourl", "viewJoinUs");
			return "login";
		}

		List<JoinUs> joinUss = InitDao.getDao().getEntities(JoinUs.class);
		model.addAttribute("joinUss", joinUss);
		return "viewJoinUss";
	}

	private void getClasses(Model model) {
		List<BigClass> bigClasses = (List<BigClass>) CacheUtils.get(Constants.NET_CACHE_ALL_BIGCLASS);
		List<SmallClass> smallClasses = (List<SmallClass>) CacheUtils.get(Constants.NET_CACHE_ALL_SMALLCLASS);
		model.addAttribute("bigClasses", bigClasses);
		model.addAttribute("smallClasses", smallClasses);
	}

	@RequestMapping(value = "uploadPic")
	public String uploadPic(HttpServletRequest request, Model model) {
		String id = getParameter(request, "id");
		MyBread bread = (MyBread) InitDao.getDao().getEntity(MyBread.class, Integer.valueOf(id));
		List<BreadPic> breadPics = breadService.findBreadPicsByBreadId(Integer.valueOf(id));
		model.addAttribute("bread", bread);
		model.addAttribute("breadPics", breadPics);
		return "uploadPic";
	}

	@RequestMapping(value = "addPrice")
	public String addPrice(HttpServletRequest request, Model model) {
		String id = getParameter(request, "id");
		MyBread bread = (MyBread) InitDao.getDao().getEntity(MyBread.class, Integer.valueOf(id));
		List<BreadPrice> breadPrices = breadService.findBreadPricesByBreadId(Integer.valueOf(id));
		model.addAttribute("bread", bread);
		model.addAttribute("breadPrices", breadPrices);
		return "addPrice";
	}

	@RequestMapping(value = "/addBread", method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> addBread(HttpServletRequest request) throws InterruptedException {
		String name = request.getParameter("name");
		String code = request.getParameter("code");
		String content = request.getParameter("content");
		String heavy = request.getParameter("heavy");
		String size = request.getParameter("size");
		String stock = request.getParameter("stock");
		String taste = request.getParameter("taste");
		String big_class_id = request.getParameter("big_class_id");
		String small_class_id = request.getParameter("small_class_id");
		String condition = request.getParameter("condition");
		String people = request.getParameter("people");
		String sendTime = request.getParameter("sendTime");
		String price = request.getParameter("price");
		String bigPicUrl = request.getParameter("bigPicUrl");
		String smallPicUrl = request.getParameter("smallPicUrl");
		String breadId = request.getParameter("breadId");

		int result = breadService.add(name, code, content, heavy, size, stock, taste, big_class_id, small_class_id, condition, people, sendTime, price, bigPicUrl, smallPicUrl, breadId);

		Map<String, Object> modelMap = new HashMap<String, Object>();
		modelMap.put("success", result);

		return modelMap;
	}

	@RequestMapping("/doUploadFile")
	public RedirectView uploadFile(HttpServletRequest request, Model model) throws Exception {
		String breadId = request.getParameter("breadId");
		String picType = request.getParameter("picType");
		MultipartHttpServletRequest fileRequest = (MultipartHttpServletRequest) request;
		MultipartFile multipartFile = fileRequest.getFile("file1");
		breadService.savePic(multipartFile, breadId, picType, request);
		getClasses(model);
		Map<String, String> map = new HashMap<String, String>();
		map.put("id", breadId);
		return new RedirectView("admin_bread");
	}

	@RequestMapping(value = "/doAddPrice", method = RequestMethod.POST)
	public RedirectView doAddPrice(HttpServletRequest request) throws InterruptedException {
		int breadId = getParameterInt(request, "breadId");
		String size = getParameter(request, "size");
		String price = getParameter(request, "price");
		breadService.addPrice(breadId, size, price);
		return new RedirectView("addPrice?id=" + breadId);
	}

	@RequestMapping(value = "/delBread", method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> delBread(HttpServletRequest request) throws InterruptedException {
		String id = getParameter(request, "id");
		int result = breadService.del(Integer.valueOf(id));
		Map<String, Object> modelMap = new HashMap<String, Object>();
		modelMap.put("success", result);
		return modelMap;
	}

	@RequestMapping(value = "/delBreadPic", method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> delBreadPic(HttpServletRequest request) throws InterruptedException {
		String id = getParameter(request, "id");
		int result = breadService.delBreadPic(Integer.valueOf(id));
		Map<String, Object> modelMap = new HashMap<String, Object>();
		modelMap.put("success", result);
		return modelMap;
	}

	@RequestMapping(value = "/delBreadPrice", method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> delBreadPrice(HttpServletRequest request) throws InterruptedException {
		String id = getParameter(request, "id");
		int result = breadService.delBreadPrice(Integer.valueOf(id));
		Map<String, Object> modelMap = new HashMap<String, Object>();
		modelMap.put("success", result);
		return modelMap;
	}

	@RequestMapping(value = "/delComment", method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> delComment(HttpServletRequest request) throws InterruptedException {
		String id = getParameter(request, "id");
		int result = breadService.delComment(Integer.valueOf(id));
		Map<String, Object> modelMap = new HashMap<String, Object>();
		modelMap.put("success", result);
		return modelMap;
	}

	@RequestMapping(value = "/deljoinUs", method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> deljoinUs(HttpServletRequest request) throws InterruptedException {
		String id = getParameter(request, "id");
		int result = breadService.deljoinUs(Integer.valueOf(id));
		Map<String, Object> modelMap = new HashMap<String, Object>();
		modelMap.put("success", result);
		return modelMap;
	}

	@RequestMapping(value = "/addComment", method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> addComment(HttpServletRequest request) throws InterruptedException {
		logger.info("addComment");
		String breadId = request.getParameter("breadId");
		String commentTitle = request.getParameter("commentTitle");
		String commentContent = request.getParameter("commentContent");
		int result = breadService.addComment(Integer.valueOf(breadId), commentTitle, commentContent, HttpUtils.getIP(request));
		Map<String, Object> modelMap = new HashMap<String, Object>();
		modelMap.put("success", result);
		return modelMap;
	}

	@RequestMapping(value = "/addCommentBase", method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> addCommentBase(HttpServletRequest request) throws InterruptedException {
		logger.info("addCommentBase");
		String commentTitle = request.getParameter("commentTitle");
		String commentContent = request.getParameter("commentContent");
		int result = breadService.addCommentBase(commentTitle, commentContent, HttpUtils.getIP(request));
		Map<String, Object> modelMap = new HashMap<String, Object>();
		modelMap.put("success", result);
		return modelMap;
	}

	@RequestMapping(value = "/addJoinUs", method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> addJoinUs(HttpServletRequest request) throws InterruptedException {
		logger.info("addJoinUs");
		String name = request.getParameter("name");
		String province = request.getParameter("province");
		String age = request.getParameter("age");
		String card = request.getParameter("card");
		String sex = request.getParameter("sex");
		String cardAddress = request.getParameter("cardAddress");
		String zipCode = request.getParameter("zipCode");
		String realAddress = request.getParameter("realAddress");
		String studyLevel = request.getParameter("studyLevel");
		String school = request.getParameter("school");
		String mobel = request.getParameter("mobel");
		String joinAddress = request.getParameter("joinAddress");
		String tel = request.getParameter("tel");
		String email = request.getParameter("email");
		String fax = request.getParameter("fax");
		String qq = request.getParameter("qq");
		String area = request.getParameter("area");
		String goMoney = request.getParameter("goMoney");
		String moneyFrom = request.getParameter("moneyFrom");
		String areaDes = request.getParameter("areaDes");
		String beforeWork = request.getParameter("beforeWork");
		String familyDes = request.getParameter("familyDes");
		String willDo = request.getParameter("willDo");
		String thinkAboutUs = request.getParameter("thinkAboutUs");
		int result = 0;
		Date now = new Date();
		Date passed5Min = DateUtils.addMinutes(now, -5);
		int count = InitDao.getDao().findEntities("from JoinUs c where c.createTime<=? and c.createTime>=?", new Object[] { now, passed5Min }).size();
		if (count > 4) {
			result = -1;
		}
		JoinUs joinUs = new JoinUs();
		joinUs.setAge(age);
		joinUs.setArea(area);
		joinUs.setAreaDes(areaDes);
		joinUs.setBeforeWork(beforeWork);
		joinUs.setCard(card);
		joinUs.setCardAddress(cardAddress);
		joinUs.setEmail(email);
		joinUs.setFamilyDes(familyDes);
		joinUs.setFax(fax);
		joinUs.setGoMoney(goMoney);
		joinUs.setJoinAddress(joinAddress);
		joinUs.setMobel(mobel);
		joinUs.setMoneyFrom(moneyFrom);
		joinUs.setName(name);
		joinUs.setProvince(province);
		joinUs.setQq(qq);
		joinUs.setRealAddress(realAddress);
		joinUs.setSchool(school);
		joinUs.setSex(sex);
		joinUs.setStudyLevel(studyLevel);
		joinUs.setTel(tel);
		joinUs.setThinkAboutUs(thinkAboutUs);
		joinUs.setWillDo(willDo);
		joinUs.setZipCode(zipCode);
		joinUs.setCreateTime(new Timestamp(System.currentTimeMillis()));
		InitDao.getDao().addEntity(joinUs);
		result = 1;

		Map<String, Object> modelMap = new HashMap<String, Object>();
		modelMap.put("success", result);
		return modelMap;
	}

}
